package cn.lym.zxxx.filter;

import cn.lym.zxxx.request.ShareHttpServletRequest;
import cn.lym.zxxx.session.ShareSessionManager;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public abstract class ShareSessionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {
        ShareSessionManager sessionManager = this.getSessionManager();
        this.addShareSessionListener(filterConfig, sessionManager);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ShareSessionManager sessionManager = this.getSessionManager();
        ShareHttpServletRequest shareHttpServletRequest = new ShareHttpServletRequest((HttpServletRequest) request, sessionManager, (HttpServletResponse) response);
        chain.doFilter(shareHttpServletRequest, response);
    }

    @Override
    public void destroy() {
    }

    protected void addShareSessionListener(FilterConfig filterConfig, ShareSessionManager sessionManager) {
        //TODO
    }

    public abstract ShareSessionManager getSessionManager();
}
